﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class OrderInfo : Form
    {
        private static OrderInfo _OrderInfo;
        private Context.Context ctx = new Context.Context();
        private static int index;
        public OrderInfo()
        {
            InitializeComponent();
        }

        public static OrderInfo Create(int index1) //单例模式
        {
            index = index1;
            if (_OrderInfo != null)
            {
                _OrderInfo.Close();
            }
            _OrderInfo = new OrderInfo();
            return _OrderInfo;
        }

        private void btnShowAllInfo_Click(object sender, EventArgs e)
        {
            var list = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text) || s.CookSpecies.Contains(txtSearchName.Text)).ToList();
            txtSearchName.Text = "";
            dgvDishInfoList.DataSource = list;
        }
        //双击dgv选中项将选中菜单信息插入订单详情表中
        private void dgvDishInfoList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //点菜
            Models.Order order = new Models.Order();
            //执行点菜操作
            try
            {
                order.TableId = index;
                order.CookId = dgvDishInfoList.SelectedRows[0].Cells[0].Value.ToString();
                order.CustomerPhone = ctx.Tables.FirstOrDefault(s => s.TableId == index).CustomerPhone;
                order.CookStatic = 0;
                ctx.Add(order);
                ctx.SaveChanges();
                dgvDishInfoList2_show();
            }
            catch (Exception ex)
            {
                return;
            }
        }
        public void dgvDishInfoList2_show()
        {
            double sum = 0;
            List<New_List> list = new List<New_List>();
            var listOrder = ctx.Orders.Where(s => s.TableId == index).ToList();
            foreach (var item in listOrder)
            {
                var tmp = ctx.Cookbooks.FirstOrDefault(s => s.CookId == item.CookId);
                New_List one = new New_List();
                one.OrderId = item.OrderId;
                one.CookId = tmp.CookId;
                one.CookName = tmp.CookName;
                one.CookPrice = (double)tmp.CookPrice;
                sum += (double)tmp.CookPrice;
                one.CookSpecies = tmp.CookSpecies;
                list.Add(one);
            }
            dgvDishInfoList2.DataSource = list;
            txtSearchName.Text = "";
            var list2 = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text) || s.CookSpecies.Contains(txtSearchName.Text)).ToList();
            dgvDishInfoList.DataSource = list2;
            sum = Math.Round(sum, 2);
            lblTotalMoney.Text = sum.ToString();
        }
        private class New_List
        {
            int orderId;
            string cookId;
            string cookName;
            string cookSpecies;
            double cookPrice;

            public int OrderId { get => orderId; set => orderId = value; }
            public string CookId { get => cookId; set => cookId = value; }
            public string CookName { get => cookName; set => cookName = value; }
            public string CookSpecies { get => cookSpecies; set => cookSpecies = value; }
            public double CookPrice { get => cookPrice; set => cookPrice = value; }
        }
        private void OrderInfo_Load(object sender, EventArgs e)
        {
            dgvDishInfoList2_show();
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(dgvDishInfoList2.SelectedRows[0].Cells[0].Value.ToString());
                Models.Order order = ctx.Orders.FirstOrDefault(s => s.OrderId == id);
                if (order.CookStatic == 1)
                {
                    MessageBox.Show("该菜品已下单不允许退菜！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                ctx.Orders.Remove(order);
                ctx.SaveChanges();
                dgvDishInfoList2_show();
            }
            catch (Exception)
            {
                return;
            }
        }

        private void btnStartOrder_Click(object sender, EventArgs e)
        {
            var listOrder = ctx.Orders.Where(s => s.TableId == index).ToList();
            foreach (var item in listOrder)
            {
                item.CookStatic = 1;
            }
            MessageBox.Show("下单成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /*

                private void FrmOrderInfo_Load(object sender, EventArgs e)
                {

                    LoadMenuType(); //加载菜单类型
                    LoadMenuList(); //加载菜单列表
                    LoadDetailInfo(); //加载订单详情
                    GetTotalMoney();

                }

                private void LoadMenuType()
                {
                    List<DishTypeInfo> list = DataTableToList.ToList<DishTypeInfo>(DishTypeInfoBll.GetDishTypeInfo());
                    list.Insert(0, new DishTypeInfo()
                    {
                        DId = 0,
                        DTitle = "全部"
                    });

                    cmbMenuType.DataSource = list;
                    cmbMenuType.DisplayMember = "DTitle";
                    cmbMenuType.ValueMember = "DId";
                }

                private void LoadMenuList()
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    if (!txtPinYin.Text.Equals(""))
                    {
                        dic.Add("DChar", txtPinYin.Text.Trim().ToString());
                    }
                    if (cmbMenuType.SelectedValue.ToString() != "0")
                    {
                        dic.Add("DTypeId", cmbMenuType.SelectedIndex + "");
                    }
                    dgvMenuInfoList.DataSource = DishInfoBll.GetDishInfo(dic);
                }


                private void cmbMenuType_SelectedValueChanged(object sender, EventArgs e)
                {
                    LoadMenuList();
                }

                private void txtPinYin_TextChanged(object sender, EventArgs e)
                {
                    LoadMenuList();
                }



                //双击dgv选中项将选中菜单信息插入订单详情表中
                private void dgvMenuInfoList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
                {
                    //点菜
                    //订单编号
                    orderId = Convert.ToInt32(this.Tag);
                    //菜单编号
                    int dishId = Convert.ToInt32(dgvMenuInfoList.SelectedRows[0].Cells[0].Value);

                    //执行点菜操作
                    if (OrderInfoBll.OrderDish(orderId, dishId))
                    {
                        //点菜成功!
                        LoadDetailInfo();
                        //计算总额
                        GetTotalMoney();
                    }
                }

                private void GetTotalMoney()
                {
                    //计算已点菜单金额
                    if (dgvOrderDetailList.Rows.Count > 0)
                    {
                        lblTotalMoney.Text = OrderInfoBll.GetTotalMoneyByOrderId(orderId).ToString();
                    }

                }

                private void LoadDetailInfo()
                {

                    orderId = Convert.ToInt32(this.Tag);

                    dgvOrderDetailList.DataSource = OrderInfoBll.OrderDetailInfo(orderId);

                    SetDgvEdits();

                }

                /// <summary>
                /// 设置前三列不可编辑
                /// </summary>
                private void SetDgvEdits()
                {
                    dgvOrderDetailList.Columns[0].ReadOnly = true;
                    dgvOrderDetailList.Columns[1].ReadOnly = true;
                    dgvOrderDetailList.Columns[2].ReadOnly = true;
                }


                //在窗体关闭时,释放窗体对象
                private void FrmOrderInfo_FormClosing(object sender, FormClosingEventArgs e)
                {
                    _frmOrderInfo = null;
                }

                //单元格失去焦点事件并且不是当前单元格时
                //private void dgvOrderDetailList_CellLeave(object sender, DataGridViewCellEventArgs e)
                //{
                //    if (e.ColumnIndex.Equals(3))
                //    {
                //        orderId = (int)dgvOrderDetailList.SelectedRows[0].Cells[0].Value;
                //        int count =Convert.ToInt32(dgvOrderDetailList.EditingControl.Text);
                //        OrderInfoBll.UpdateCountByOId(orderId, count);
                //    }

                //}

                /// <summary>
                /// 单选定单元格停止编辑时发生的事件   ==>修改菜品数量
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void dgvOrderDetailList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
                {
                    if (e.ColumnIndex.Equals(3))
                    {
                        //获取当前已点菜品订单详情编号
                        int oId = (int)dgvOrderDetailList.SelectedRows[0].Cells[0].Value;
                        int count = (int)dgvOrderDetailList.SelectedRows[0].Cells[3].Value;
                        if (OrderInfoBll.UpdateCountByOId(oId, count) > 0)
                        {
                            MessageBox.Show("更新成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //计算总金额
                            GetTotalMoney();
                        }
                        else MessageBox.Show("更新失败!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }

                private void btnDeleteItem_Click(object sender, EventArgs e)
                {
                    try
                    {
                        int oId = (int)dgvOrderDetailList.SelectedRows[0].Cells[0].Value;
                        DialogResult result = MessageBox.Show("是否删除?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (result.Equals(DialogResult.OK))
                        {
                            if (OrderInfoBll.DeleteSelectedItem(oId) > 0)
                            {
                                MessageBox.Show("删除成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                LoadDetailInfo(); //加载订单详情
                                GetTotalMoney();
                            }
                            else MessageBox.Show("删除失败!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }
                    }
                    catch
                    {
                        MessageBox.Show("当前没有点菜!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                private void btnStartOrder_Click(object sender, EventArgs e)
                {
                    if (OrderInfoBll.SetOrderMoney(Convert.ToInt32(this.Tag), Convert.ToDecimal(lblTotalMoney.Text.ToString())))
                    {
                        MessageBox.Show("下单成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }*/
    }
}
